package com.softstao.guoyu.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.model.Code;
import com.softstao.guoyu.mvp.interactor.ForgetPassInteractor;
import com.softstao.guoyu.mvp.presenter.ForgetPassPresenter;
import com.softstao.guoyu.mvp.viewer.ChangePsdCodeViewer;
import com.softstao.softstaolibrary.library.wechatpay.MD5Util;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/24.
 */

public class ForgetPassActivity extends BaseActivity implements ChangePsdCodeViewer{

    @AIPresenter(presenter = ForgetPassPresenter.class,interactor = ForgetPassInteractor.class)
    ForgetPassPresenter presenter;

    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.regret_btn)
    TextView regretBtn;
    private String codeNum;
    private Timer timer;
    private int countTime;

    @Override
    public int _ContentView() {
        return R.layout.activity_forget_psd;
    }

    @Override
    public void initView() {
        initTitle("找回密码");
        titleBar.getDivider().setVisibility(View.GONE);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if (msg.what > 0) {
                regretBtn.setTextColor(getResources().getColor(R.color.font_gray));
                regretBtn.setEnabled(false);
                regretBtn.setText("(" + msg.what + ")获取验证码");
            } else {
                regretBtn.setEnabled(true);
                regretBtn.setTextColor(getResources().getColor(R.color.font_pink));
                regretBtn.setText("获取验证码");
                timer.cancel();
            }
        }
    };

    @OnClick({R.id.del_btn, R.id.regret_btn, R.id.next_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.del_btn:
                mobile.setText("");
                break;
            case R.id.regret_btn:
                if (!checkMobile()){
                    return;
                }
                sendCode();
                break;
            case R.id.next_btn:
                if (!check()){
                    return;
                }
                Intent intent = new Intent(context,SetPasswordActivity.class);
                intent.putExtra("mobile",mobile.getText().toString());
                intent.putExtra("code",code.getText().toString());
                startActivity(intent);
                finish();
                break;
        }
    }

    private boolean checkMobile(){
        if (TextUtils.isEmpty(mobile.getText().toString())){
            LZToast.getInstance(context).showToast("请输入手机号");
            return false;
        }
//        if (LZUtils.isMobile(mobile.getText().toString()) == false) {
//            LZToast.getInstance(context).showToast("手机号不合法");
//            return false;
//        }
        return true;
    }

    private boolean check(){
        if (TextUtils.isEmpty(mobile.getText().toString())){
            LZToast.getInstance(context).showToast("请输入手机号");
            return false;
        }
//        if (LZUtils.isMobile(mobile.getText().toString()) == false) {
//            LZToast.getInstance(context).showToast("手机号不合法");
//            return false;
//        }
        if (TextUtils.isEmpty(code.getText().toString())) {
            LZToast.getInstance(context).showToast("请输入验证码");
            return false;
        }
        if (TextUtils.equals(MD5Util.MD5Encode(code.getText().toString() + "047c74874ec1de7bed9867a93701ed3d",null),codeNum) == false) {
            LZToast.getInstance(context).showToast("验证码不正确");
            return false;
        }
        return true;
    }

    @Override
    public void sendCode() {
        presenter.sendCode(mobile.getText().toString());
        regretBtn.setEnabled(false);
    }

    @Override
    public void sendResult(Code code) {
        codeNum = code.getCode();
        countTime = 60;
        timer = new Timer();
        if (!TextUtils.isEmpty(mobile.getText())) {
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    countTime--;
                    handler.sendEmptyMessage(countTime);
                }
            }, 1000, 1000);
        }
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        regretBtn.setText("获取验证码");
        regretBtn.setEnabled(true);
        regretBtn.setTextColor(getResources().getColor(R.color.font_pink));
    }
}
